package com.CST.service.impl.zx;

import com.CST.common.BaseEntity;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import com.CST.dto.zx.ZxUserProjectRelationDTO;
import com.CST.dto.zx.ZxUserProjectRelationPageDTO;
import com.CST.dto.zx.addUserDTO;
import com.CST.entity.zx.ZxUserProjectRelation;
import com.CST.mapper.cst.zx.ZxUserProjectRelationMapper;
import com.CST.service.zx.ZxUserProjectRelationService;
import com.CST.vo.zx.ZxProjectTypeList;
import com.CST.vo.zx.ZxUserListVO;
import com.CST.vo.zx.ZxUserProjectRelationVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qtx
 * @since 2023-06-07
 */
@Service
public class ZxUserProjectRelationServiceImpl extends ServiceImpl<ZxUserProjectRelationMapper, ZxUserProjectRelation> implements ZxUserProjectRelationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ZxUserProjectRelationDTO dto) {
        ZxUserProjectRelation build = ZxUserProjectRelation.builder()
                .userId(dto.getUserId())
                .userName(dto.getUserName())
                .entryCode(dto.getEntryCode())
                .build();
        return save(build);
    }

    @Override
    public boolean removeByIdNew(Long id) {
        boolean b = removeById(id);
        remove(Wrappers.lambdaQuery(ZxUserProjectRelation.class).eq(ZxUserProjectRelation::getId, id));
        return b;
    }

    @Override
    public IPage<ZxUserProjectRelationVO> mainPage(ZxUserProjectRelationPageDTO dto) {
        Page<ZxUserProjectRelationVO> page = dto.getPage();
        return baseMapper.selectPageNew(page,dto);
    }

    @Override
    public List<ZxUserListVO> userList(addUserDTO dto) {
        List<ZxUserListVO>   zxUserListVOList=new ArrayList<>();
        List<String> roleId = baseMapper.getRoleId();

        // 添加权限人员
        List<ZxUserListVO> zxUserListVOS = baseMapper.userList(dto);
        for (ZxUserListVO zxUserListVO : zxUserListVOS) {
            if (StringUtils.isNotEmpty(zxUserListVO.getRoleId())){
                List<String> list = new ArrayList<>(Arrays.asList(zxUserListVO.getRoleId().split(",")));
                List<String> roleIdCopy = new ArrayList<>(roleId);
             //取2个集合的交集
                roleIdCopy.retainAll(list);
                if (!roleIdCopy.isEmpty()) {
                    zxUserListVOList.add(zxUserListVO);

                }

            }
        }


        return zxUserListVOList;
    }

    @Override
    public List<ZxProjectTypeList> projectList(String entryCode) {
        return baseMapper.projectList(entryCode);
    }
}
